<template>
    <section>
        <el-row>
            <el-col :span="24">
                <el-table :data="list" @selection-change="select">
                    <el-table-column
                        type="selection"
                        width="55">
                    </el-table-column>
                    <el-table-column prop="id" label="id" width="40">
                    </el-table-column>
                    <el-table-column prop="desc" label="描述" width="120">
                    </el-table-column>
                    <el-table-column prop="click" label="点击数" width="80">
                    </el-table-column>
                    <el-table-column prop="href" label="跳转地址" min-width="120">
                    </el-table-column>
                    <el-table-column prop="order" label="顺序" width="80">
                    </el-table-column>
                    <el-table-column prop="created_at" label="创建时间" width="140">
                    </el-table-column>
                    <el-table-column prop="aaaa" label="图片" min-width="120">
                        <template slot-scope="scope">
                            <el-button @click="detail(scope.row)">查看</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column prop="aaaa" label="操作" min-width="120">
                        <template slot-scope="scope">
                            <el-button @click="edit(scope.row)">修改</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>

        <div style="padding-top: 10px">
            <el-button type="primary" @click="edit">新增</el-button>
            <el-button type="danger" @click="del">删除</el-button>
        </div>

        <el-dialog
            title="图片预览"
            :visible.sync="dialogVisible"
            width="30%">
            <img :src="current" alt="图片预览" style="width: 100%">
            <span>
            <el-button @click="dialogVisible = false">关闭</el-button>
            </span>
        </el-dialog>
        <el-dialog
            title="编辑信息"
            :visible.sync="addFormVisible"
            width="30%">
            <el-form v-model="carousel" label-width="80" :rules="rule" ref="carousel">
                <el-form-item label="输入网址" prop="href">
                    <el-input v-model="carousel.href" placeholder="不输入则图片不可点击"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="desc">
                    <el-input v-model="carousel.desc" placeholder="默认为空"></el-input>
                </el-form-item>
                <el-form-item label="输入图片Url" prop="imgUrl">
                    <el-input v-model="carousel.imgUrl" placeholder="可以选择上传图片,不填写使用默认"></el-input>
                </el-form-item>
                <!-- <div>
                     <input type="file" name="上传图片" id="www" style="visibility: hidden; position: absolute">
                     <label for="www" class="el-button">选择图片</label>
                     <div>(点击删除)</div>
                 </div>-->
                <el-upload
                    class="wallpaper-uploader"
                    action="http://localhost:8000/file/upload/avatar"
                    :show-file-list="false"
                    :before-upload="setPic">
                    <el-button v-if="!carousel.imgUrl">选择图片</el-button>
                    <img class="wallpaper" :src="carousel.imgUrl" v-if="carousel.imgUrl" title="点击更换">
                </el-upload>
            </el-form>
            <div style="text-align: center">
                <el-button @click="addFormVisible = false">关闭</el-button>
                <el-button @click="Editsubmit">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>
<script>
    /* eslint-disable indent,space-before-function-paren,spaced-comment */
    import {deleteCarouse, getCarouselList, getImageUrl, UpdateCarouse, AddCarouse} from '../../api/api'
    import {Upload} from '../../public/util'

    export default {
        data() {
            return {
                list: [],
                dialogVisible: false,
                addFormVisible: false,
                current: {},
                currentSelect: [],
                carousel: {
                    imgUrl: '',
                    href: '',
                    desc: ''
                },
                rule: {
                    imgUrl: [
                        // {required: true, message: '请输入Url或上传图片', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            Init() {
                getCarouselList().then(
                    data => {
                        if (data.status) {
                            this.list = data.data
                        } else {
                            this.$message.error(data.msg)
                        }
                    }
                ).catch()
            },
            detail(a) {
                this.current = a.imgUrl
                this.dialogVisible = true
            },
            del() {
                this.$confirm('此操作将永久删除该, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteCarouse({
                        id: this.currentSelect.map(x => x.id).join(',')
                    }).then(
                        data => {
                            if (data.status) {
                                this.$message.success('删除成功')
                                this.Init()
                            } else {
                                this.$message.error(data.msg)
                            }
                        }
                    ).catch()
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
            },
            select(row) {
                this.currentSelect = row
            },
            edit(a = false) {
                if (a) {
                    this.carousel = {
                        id: a.id,
                        imgUrl: a.imgUrl,
                        href: a.href,
                        desc: a.desc
                    }
                } else {
                    this.carousel = {
                        imgUrl: '',
                        href: '',
                        des: ''
                    }
                }
                this.addFormVisible = true
            },
            setPic(file) {
                var _this = this
                Upload(file, 'carousel', function (x) {
                    // _this.imgUrl = getImageUrl(x)
                    _this.carousel.imgUrl = getImageUrl(x)
                })
                return false
            },
            Editsubmit() {
                var para = {
                    ...this.carousel
                }
                if (para.id) {
                    UpdateCarouse(para).then(
                        data => {
                            this.Init()
                            this.addFormVisible = false
                        }
                    ).catch()
                } else {
                    AddCarouse(para).then(
                        data => {
                            this.Init()
                            this.addFormVisible = false
                        }
                    ).catch()
                }
            }
        },
        mounted() {
            this.Init()
        }
    }
</script>
<style scoped>
    section {
        /*background: red;*/
    }

    .avatar {
        width: 50px;
        height: 50px;
        border-radius: 50%;
    }

    .wallpaper {
        width: 100%;
        border-radius: 8px;
    }
</style>
